<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>toolTip跟随鼠标移动</title>
  <style>
    .box {
      position: relative;
      width: 80%;
      margin: 50px auto;
    }
    .rate-tip{
  /* top:-36px; */
  width:auto;
  position:absolute;
  display: none;
  padding: 10px;
  color: #fff;
  background: #303133;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.2;
  transition:.2s;
  text-align:left;
}
.rate-tip .trangle{
  position:absolute;
  border:6px solid transparent;
  border-top-color:#303133;
  bottom: -12px;
  left: 50%;
  margin-left: -6px;
}
.deviceToolTip {
  width: 100%;
  height: 6px;
  background: palevioletred;
  border-radius: 6px;
  overflow: hidden;
  cursor:pointer;
}
  </style>
</head>

<body>
  <div class="box">
    <div class="rate-tip">
      <div class="trangle"></div>
      <div>no data</div>
    </div>
    <div class="deviceToolTip">
    </div>
  </div>
  <div class="box">
    <div class="rate-tip">
      <div class="trangle"></div>
      <div>no data</div>
    </div>
    <div class="deviceToolTip">
    </div>
  </div>
  <div class="box">
    <div class="rate-tip">
      <div class="trangle"></div>
      <div>no data</div>
    </div>
    <div class="deviceToolTip">
    </div>
  </div>
  <div class="box">
    <div class="rate-tip">
      <div class="trangle"></div>
      <div>no data</div>
    </div>
    <div class="deviceToolTip">
    </div>
  </div>
  <div class="box">
    <div class="rate-tip">
      <div class="trangle"></div>
      <div>no data</div>
    </div>
    <div class="deviceToolTip">
    </div>
  </div>
</body>
<script>
    let tips = document.querySelectorAll(".deviceToolTip")
          let tipsArr = [...tips].filter(item => item.clientWidth!=0)
          let left = document.querySelector('.box').offsetLeft;
          // console.log(tipsArr.length)
          for (const item of tipsArr) {
            // let top = item.getBoundingClientRect().top;
            // console.log(top)
            let sibling = item.previousElementSibling;
            item.addEventListener('mouseover',()=>{
              sibling.style.display = 'block'
              // console.log(sibling)
              
              // sibling.style.minWidth = sibling.clientWidth + 'px'
              // sibling.style.minWidth = sibling.children[1].clientWidth + 20+ 'px'
              sibling.style.top = -(sibling.clientHeight + 10) + 'px'
            })
            item.addEventListener('mousemove',(e) => {
              // console.log(e)
              // console.log(sibling,sibling.parentElement.clientWidth,sibling.offsetLeft,sibling.clientWidth)
              if(sibling.parentElement.clientWidth - (e.screenX - left - sibling.clientWidth/2) - sibling.clientWidth <= 0) {
                sibling.style.left = sibling.parentElement.clientWidth - sibling.clientWidth - 10 + 'px';
              }else if(e.screenX - left - sibling.clientWidth/2 <=0){
                sibling.style.left = 0;
              }else{
                sibling.style.left = e.screenX - left - sibling.clientWidth/2 + 'px'
              }
              // sibling.style.top = top - 40 + 'px'
            })
            item.addEventListener('mouseout',()=>{
              sibling.style.display = 'none'
            })
          }
</script>
</html>